"""
Handle a OWASP funded Order payments.
"""
from django.utils.translation import ugettext as _
from payment.modules.base import BasePaymentProcessor, ProcessorResult
from decimal import Decimal

class PaymentProcessor(BasePaymentProcessor):

    def __init__(self, settings):
        super(PaymentProcessor, self).__init__('owasp_payments', settings)

    def can_refund(self):
        return True

    def can_authorize(self):
        return True

    def prepare_data(self, order):
        super(PaymentProcessor, self).prepare_data(order)

    def authorize_payment(self, order=None, amount=None, testing=False):
        """Authorize a single payment.

        Returns: ProcessorResult
        """
        if order:
            self.prepare_data(order)
        else:
            order = self.order

        if order.paid_in_full:
            self.log_extra('%s is paid in full, no authorization attempted.', order)
            results = ProcessorResult(self.key, True, _("No charge needed, paid in full."))
        else:
            self.log_extra('Authorizing payment of %s for %s', amount, order)
        
        ''' SEND EMIAL HERE FOR AUTH NOTIFICATION'''
            
        payment = self.record_authorization(order=self.order, amount=amount,
                    transaction_id=self.order.owasppayment.id, reason_code="0")

        return ProcessorResult(self.key, True, _('Success'), payment)

    def capture_authorized_payment(self, authorization, testing=False, order=None, amount=None):
        """Capture a single payment"""
        if order:
            self.prepare_data(order)
        else:
            order = self.order

        if order.authorized_remaining == Decimal('0.00'):
            self.log_extra('No remaining authorizations on %s', order)
            return ProcessorResult(self.key, True, _("Already complete"))

        self.log_extra('Capturing Authorization #%i for %s', authorization.id, order)
        
        payment = self.record_payment(transaction_id=self.order.owasppayment.id, reason_code="0", authorization=authorization, amount=amount, order=order)

        return ProcessorResult(self.key, True, _('Success'), payment)

def capture_payment(self, testing=False, order=None, amount=None):
        #we will never use this, but it has to be here.....
        if not order:
            order = self.order

        if amount is None:
            amount = order.balance

        payment = self.record_payment(order=order, amount=amount,
            transaction_id=self.order.owasppayment.id, reason_code='0')

        return ProcessorResult(self.key, True, _('Success'), payment)
